﻿
$(function() {

    jQuery.extend(jQuery.validator.messages, {
        required: "*",
        remote: "请修正该字段",
        email: "请输入正确格式的电子邮件",
        url: "请输入合法的网址",
        date: "请输入合法的日期",
        dateISO: "请输入合法的日期 (ISO).",
        number: "请输入合法的数字",
        digits: "只能输入整数",
        creditcard: "请输入合法的信用卡号",
        equalTo: "请再次输入相同的值",
        accept: "请输入拥有合法后缀名的字符串",
        maxlength: jQuery.format("请输入一个长度最多是 {0} 的字符串"),
        minlength: jQuery.format("请输入一个长度最少是 {0} 的字符串"),
        rangelength: jQuery.format("请输入一个长度介于 {0} 和 {1} 之间的字符串"),
        range: jQuery.format("请输入一个介于 {0} 和 {1} 之间的值"),
        max: jQuery.format("请输入一个最大为 {0} 的值"),
        min: jQuery.format("请输入一个最小为 {0} 的值")
    });


    $(".form").validate(
            { errorClass: "msg" }
            );

    $(".tabs").tabs();

    //    $("tr").hover(
    //      function() {
    //        
    //          $(this).addClass("hover");
    //      },
    //      function() {
    //          $(this).removeClass("hover");
    //      }
    //    );
});

jQuery.extend({
    highlight: function(node, re, nodeName, className) {
        if (node.nodeType === 3) {
            var match = node.data.match(re);
            if (match) {
                var highlight = document.createElement(nodeName || 'span');
                highlight.className = className || 'highlight';
                var wordNode = node.splitText(match.index);
                wordNode.splitText(match[0].length);
                var wordClone = wordNode.cloneNode(true);
                highlight.appendChild(wordClone);
                wordNode.parentNode.replaceChild(highlight, wordNode);
                return 1; //skip added node in parent
            }
        } else if ((node.nodeType === 1 && node.childNodes) && // only element nodes that have children
                !/(script|style)/i.test(node.tagName) && // ignore script and style nodes
                !(node.tagName === nodeName.toUpperCase() && node.className === className)) { // skip if already highlighted
            for (var i = 0; i < node.childNodes.length; i++) {
                i += jQuery.highlight(node.childNodes[i], re, nodeName, className);
            }
        }
        return 0;
    }
});

jQuery.fn.unhighlight = function(options) {
    var settings = { className: 'highlight', element: 'span' };
    jQuery.extend(settings, options);

    return this.find(settings.element + "." + settings.className).each(function() {
        var parent = this.parentNode;
        parent.replaceChild(this.firstChild, this);
        parent.normalize();
    }).end();
};

jQuery.fn.highlight = function(words, options) {
    if (words == "") {
        return;
    }
    var settings = { className: 'highlight', element: 'span', caseSensitive: false, wordsOnly: false };
    jQuery.extend(settings, options);

    if (words.constructor === String) {
        words = [words];
    }

    var flag = settings.caseSensitive ? "" : "i";
    var pattern = "(" + words.join("|") + ")";
    if (settings.wordsOnly) {
        pattern = "\\b" + pattern + "\\b";
    }
    var re = new RegExp(pattern, flag);

    return this.each(function() {
        jQuery.highlight(this, re, settings.element, settings.className);
    });
};


function CheckAll(id, name) {
    $("INPUT[name=" + name + "][type='checkbox']").attr('checked', $('#' + id).is(':checked'));
}

function CheckAllT(name) {
    $("INPUT[name=" + name + "][type='checkbox']").attr('checked', true);
}

function InterView(id, postid) {
    if (id == null) { id = 0; }
    if (postid == null || postid == "") {
        alert("请选择申请的岗位!");
        return;
    }
    if (id == 0) {
        var list = new Array();
        $("INPUT[name='cb'][type='checkbox'][checked]").each(function() {
            list.push($(this).val());
        });
        if (list.length >= 1) {
            $.post("/company/CompanyApplyInterViews.aspx", { "id": list, "postid": postid }, function(data) { alert(data); });
        }
        else {
            alert("请选择人员!");
        }
    }
    else {
        $.post("/company/CompanyApplyInterView.aspx", { "resumeid": id, "postid": postid }, function(data) { alert(data); });
    }
}

function InterViewNoPost(id) {
    if (id == null) { id = 0; }
    if (id == 0) {
        var list = new Array();
        $("INPUT[name='cb'][type='checkbox'][checked]").each(function() {
            list.push($(this).val());
        });
        if (list.length >= 1) {
            $.post("/company/ApplyInterView.aspx", { "id": list }, function(data) { alert(data); });
        }
        else {
            alert("请选择人员!");
        }
    }
    else {
        $.post("/company/ApplyInterView.aspx", { "id": id }, function(data) { alert(data); });
    }
}

function AddCompanyResumeLib(id) {
    if (id == null) { id = 0; }
    if (id == 0) {
        var list = new Array();
        $("INPUT[name='cb'][type='checkbox'][checked]").each(function() {
            list.push($(this).val());
        });
        if (list.length >= 1) {
            $.post("/company/CompanyAddCompanyResumeLib.aspx", { "id": list }, function(data) { alert(data); });
        }
        else {
            alert("请选择人员!");
        }
    }
    else {
        $.post("/company/CompanyAddCompanyResumeLib.aspx", { "id": id }, function(data) { alert(data); });
    }
}


function AddToCompanyResumeLib(id) {
    if (id == null) { id = 0; }
    if (id == 0) {
        var list = new Array();
        $("INPUT[name='cb'][type='checkbox'][checked]").each(function() {
            list.push($(this).val());
        });
        if (list.length >= 1) {
            $.post("/company/AddCompanyResumelib.aspx", { "id": list }, function(data) { alert(data); });
        }
        else {
            alert("请选择人员!");
        }
    }
    else {
        $.post("/company/AddCompanyResumelib.aspx", { "id": id }, function(data) { alert(data); });
    }
}

function delPersonResume() {
    if (confirm("确定要删除吗?")) {
        var list = new Array();
        $("INPUT[name='cb'][type='checkbox'][checked]").each(function() {
            list.push($(this).val());
        });
        if (list.length >= 1) {
            $.post("/company/DelPersonResume.aspx", { "id": list }, function(data) { alert(data); window.location.href = window.location.href });
        }
        else {
            alert("请选择人员!");
        }
    }
}

function ApplyPosts(id) {
    if (id == null) {
        var list = new Array();
        $("INPUT[name='cb'][type='checkbox'][checked]").each(function() {
            list.push($(this).val());
        });
        if (list.length >= 1) {
            $.post("/Person/ApplyPosts.aspx", { "id": list }, function(data) { alert(data); });
        }
        else {
            alert("请选择一个职位!");
        }
    }
    else {
        $.post("/Person/ApplyPosts.aspx", { "id": id }, function(data) { alert(data); });
    }
}

function AddtoFavorites(id) {
    if (id == null) {
        var list = new Array();
        $("INPUT[name='cb'][type='checkbox'][checked]").each(function() {
            list.push($(this).val());
        });
        if (list.length >= 1) {
            $.post("/Person/AddtoFavorites.aspx", { "id": list }, function(data) { alert(data); });
        }
        else {
            alert("请选择一个职位!");
        }
    }
    else {
        $.post("/Person/AddtoFavorites.aspx", { "id": id }, function(data) { alert(data); });
    }

}

function ViewthePosts(id) {
    if (id == null) {
        var list = new Array();
        $("INPUT[name='cb'][type='checkbox'][checked]").each(function() {
            list.push($(this).val());
        });
        if (list.length >= 1) {
            window.open("/post/PreviewPost.aspx/" + list);
        }
        else {
            alert("请选择一个职位!");
        }
    }
    else {
        window.open("/post/PreviewPost.aspx/" + id);
    }
}



function ApplyPersonFavorite(id) {
    if (id == null) {
        var list = new Array();
        $("INPUT[name='cb'][type='checkbox'][checked]").each(function() {
            list.push($(this).val());
        });
        if (list.length >= 1) {
            $.getJSON("/Person/ApplyPersonFavorite.aspx", { "fid": list }, function(data) { if (data[0] == "1") { alert(data[1]); window.location.href = window.location.href; } else { alert(data[1]); } });
        }
        else {
            alert("请选择一个!");
        }
    }
    else {
        $.getJSON("/Person/ApplyPersonFavorite.aspx", { "fid": id }, function(data) { if (data[0] == "1") { alert(data[1]); window.location.href = window.location.href; } else { alert(data[1]); } });
    }
}


function login() {
    if ($("input[name='username']").val() == "") {
        alert("请输入用户名!");
    }
    else if ($("input[name='password']").val() == "") {
        alert("请输入密码!");
    }
    else if ($("input[name='CheckCode']").val() == "") {
        alert("请输入验证码!");
    }
    else {
        $.getJSON("/ral/login.aspx?username=" +
        $("input[name='username']").val() + "&password=" +
        $("input[name='password']").val() + "&checkcode=" +
        $("input[name='CheckCode']").val() + "&timestamp=" + new Date().valueOf(), function(data) {
            if (data[0] == 1) { eval(data[1]); }
            else { alert(data[1]); }
        });
    }
    return false;

}



function emailLogin() {
    if ($("#emailName").val() == "") {
        alert("请输入用户名!");
    }
    else if ($("#emailPassword").val() == "") {
        alert("请输入密码!");
    }
    else {
        $.getJSON("/ral/emaillogin.aspx",
        { "name": $("#emailName").val(), "password": $("#emailPassword").val(), "emailType": $("#emailType").val() },
         function(data) { if (data[0] == "1") { eval(data[1]); } else { alert(data[1]); } });
    }
}

Date.prototype.format = function(format) {
    var o = {
        "M+": this.getMonth() + 1, //month
        "d+": this.getDate(),    //day
        "h+": this.getHours(),   //hour
        "m+": this.getMinutes(), //minute
        "s+": this.getSeconds(), //second
        "q+": Math.floor((this.getMonth() + 3) / 3),  //quarter
        "S": this.getMilliseconds() //millisecond
    }
    if (/(y+)/.test(format)) format = format.replace(RegExp.$1,
 (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o) if (new RegExp("(" + k + ")").test(format))
        format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
    return format;
}